const express = require('express');
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
const passport = require('passport');       //根据token验证获取数据
const cors =require('cors');
const app = express();

//引入
const users = require('./routes/api/users');
const blogs = require('./routes/api/blogs');
const catagory = require('./routes/api/catagory');
const upload = require('./routes/api/upload');
const country = require('./routes/api/country');
// const articlelists = require('./routes/api/articlelist');
// const like = require('./routes/api/like');
// const up = require('./routes/api/up');
// const tags = require('./routes/api/tags');
// const message = require('./routes/api/message');
// const messagechildren = require('./routes/api/messageChildren');
// const about = require('./routes/api/about');
// const resume = require('./routes/api/resume');
// const upload = require('./routes/api/upload');
//db 配置
const db = require('./config/db.js').mongoUrl;


//使用body-parser中间件
// app.use(bodyParser.json());
// app.use(bodyParser.urlencoded({extended:false}));
app.use(bodyParser.json({limit: '50mb'}));
app.use(bodyParser.urlencoded({limit: '50mb', extended: true, parameterLimit: 1000000}));

//连接数据库
mongoose.connect(db,{ useNewUrlParser: true }).then(()=>{
    //这个设置后解决警告DeprecationWarning: collection.findAndModify is deprecated. Use findOneAndUp
    //弃用的警告是由于MongoDB节点的变化.使用MongoDB默认的findAndModify()，findAndModify（）已经启用
    mongoose.set('useFindAndModify', false);
    console.log('数据库连接成功')
}).catch(err=>{
    console.log(err)
});

//passport 初始化
app.use(passport.initialize());
app.use(cors());


require('./config/passport')(passport);


//设置跨域访问
// app.all('*', (req, res, next) => {
//     res.header("Access-Control-Allow-Origin", "*");
//     res.header("Access-Control-Allow-Headers", "X-Requested-With");
//     res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
//     res.header("X-Powered-By", ' 3.2.1');
//     res.header("Content-Type", "application/json;charset=utf-8");
//     next();
// });
// 使用 routers
app.use('/api/users',users);
app.use('/api/blogs',blogs);
app.use('/api/catagory',catagory);
app.use('/api/upload',upload);
app.use('/api/country',country);

// app.use('/api/articlelist',articlelists);
// app.use('/api/like',like);
// app.use('/api/zan',up);
// app.use('/api/tags',tags);
// app.use('/api/message',message);
// app.use('/api/messagechildren',messagechildren);
// app.use('/api/upload',upload);
// app.use('/api/about',about);
// app.use('/api/resume',resume);

const  port = process.env.PORT || 8000;

app.listen(port,()=>{
    console.log(`Service running on port ${port}`)
});
